package com.ieei.client;

import java.util.Map;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ieei.iService.HelloWorld;
import com.ieei.server.User;
import com.ieei.server.UserImpl;

public final class Client {

	private static final QName SERVICE_NAME = new QName(
			"http://server.ieei.com/", "HelloWorld");
	private static final QName PORT_NAME = new QName("http://server.ieei.com/",
			"HelloWorldPort");

	private Client() {
	}

	public static void main(String args[]) throws Exception {
		t2();
	}

	public static void t2() {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "client.xml" });

		HelloWorld client = (HelloWorld) context.getBean("client");

		String response = client.sayHi("Joe");
		System.out.println("Response: " + response);
		System.exit(0);
	}

	public static void t1() {
		Service service = Service.create(SERVICE_NAME);
		String endpointAddress = "http://localhost:8090/cxf/services/hello_world";
		service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING,
				endpointAddress);

		HelloWorld hw = service.getPort(HelloWorld.class);
		System.out.println(hw.sayHi("World"));

		User user = new UserImpl("World");
		System.out.println(hw.sayHiToUser(user));

		user = new UserImpl("Galaxy");
		System.out.println(hw.sayHiToUser(user));

		user = new UserImpl("Universe");
		System.out.println(hw.sayHiToUser(user));

		System.out.println();
		System.out.println("Users: ");
		Map<Integer, User> users = hw.getUsers();
		for (Map.Entry<Integer, User> e : users.entrySet()) {
			System.out.println("  " + e.getKey() + ": "
					+ e.getValue().getName());
		}
	}

}
